from rest_framework.mixins import CreateModelMixin, DestroyModelMixin
from rest_framework.viewsets import GenericViewSet

from apps.models import Notice
from apps.serializers.notice import NoticeSerializer

from apps.utils.common_mixin import APIListModelMixin, APICreateModelMixin
from apps.utils.common_response import APIResponse
from apps.views.main.user import make_login_log


class AdminNoticeView(GenericViewSet, APIListModelMixin, APICreateModelMixin):
    queryset = Notice.objects.all()
    serializer_class = NoticeSerializer

class AdminNoticeDeleteView(GenericViewSet):
    def create(self, request):
        try:
            ids = request.GET.get('ids')
            ids_arr = ids.split(',')
            # 批量删除
            Notice.objects.filter(id__in=ids_arr).delete()
        except Notice.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        return APIResponse(code=0, msg='删除成功')

